/* eslint-disable node/prefer-global/process */
import type { ConfigEnv, UserConfig } from 'vite';
import { resolve } from 'node:path';
import vue from '@vitejs/plugin-vue';
import AutoImport from 'unplugin-auto-import/vite';
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers';
import Components from 'unplugin-vue-components/vite';
import { loadEnv } from 'vite';

function pathResolve(dir: string) {
  return resolve(process.cwd(), '.', dir);
}

export default (configEnv: ConfigEnv): UserConfig => {
  const { mode } = configEnv;
  const env = loadEnv(mode, pathResolve('env'), 'LMS_');
  const port = Number.parseInt(env.LMS_APP_PORT);
  return {
    resolve: {
      alias: [
        {
          find: '@',
          replacement: `${pathResolve('src')}/`,
        },
      ],
    },
    envDir: './env',
    envPrefix: 'LMS_APP',
    plugins: [
      vue(),
      AutoImport({
        imports: [
          'vue',
          {
            'lodash-es': [
              'isString',
              'isArray',
              'isEmpty',
              'cloneDeep',
              'debounce',
              'merge',
              'uniq',
              'throttle',
              'values',
              'keys',
              'camelCase',
            ],
          },
        ],
        dts: 'src/types/auto-imports.d.ts',
      }),
      Components({
        resolvers: [
          AntDesignVueResolver({
            importStyle: false,
          }),
        ],
      }),
    ],
    server: {
      host: true,
      port,
      open: false,
    },
  };
};
